<html>
<head>
  <title>Detailed view for station</title>
  <script type="text/javascript">
  function showHideDiv(divID)
  {
    var div = document.getElementById(divID);
    if(div.style.display == 'block')
      div.style.display = 'none';
    else
      div.style.display = 'block';
  }
  </script>
</head>

<body>
  <?php
    include("../header.php");
    $id = $_GET['computer_id'];
  ?>

  <div>
    <form id="detailsForm" name="detailsForm" <?php echo 'action="./ComputerDetails.php?computer_id=' .$id. '&action=saveChanges"'; ?> method="get">
      
      <?php 
        $con = get_connection();

        if(isset($_GET['action']))
        {
          $action = $_GET['action'];
          switch($action)
          {
            case 'saveChanges' :
              $computerName = $_GET['name'];
              $roomID = $_GET['room_id'];
              $op = $_GET['operational'];
              $server = $_GET['isServer'];
              
              $query = "call UpdateComputerDetails(" .$id. ", '" .$computerName. "', " .$roomID. ", " .$op. ", " .$server. ")";

              mysql_query($query) or die(mysql_error());
              echo '<div style="color: Green; font-weight: bold;">Computer details saved successfully...</div><br />';
            break;
            case 'newComputer' :
              $name = $_GET['name'];
              $roomID = $_GET['room_id'];
              $server = $_GET['isServer'];
              $price = (strlen($_GET['price'])>0 ? $_GET['price'] : 0);
              $config = $_GET['configuration'];
              $purchase = $_GET['purchaseDate'];
              $warranty = (strlen($_GET['warranty'])>0 ? $_GET['warranty'] : 0);
              $supplier = $_GET['supplier'];
              
              $query = "AddNewComputer('" .$name. "', " .$roomID. ", " .$server. ", " .$price. ", '" .$config. "', '" .$purchase.
                       "', " .$warranty .", '" .$supplier. "')";

              $id = mysql_call_func($query);
            break;
            case 'addHW' :
              mysql_query('call AddHWComponentToComputer(' .$_GET['computer_id']. ', ' .$_GET['component_id']. ')')
              or die(mysql_error());
            break;
            case 'removeHW' :
              mysql_query('call RemoveHWComponentOfComputer(' .$_GET['computer_id']. ', ' .$_GET['component_id']. ')')
              or die(mysql_error());
            break;
            case 'addSW' :
              mysql_query('call AddSoftwareToComputer(' .$_GET['computer_id']. ', ' .$_GET['software_id']. ')')
              or die(mysql_error());
            break;
            case 'removeSW' :
              mysql_query('call RemoveSoftwareOfComputer(' .$_GET['computer_id']. ', ' .$_GET['software_id']. ')')
              or die(mysql_error());
            break;
            case 'addDomain' :
              mysql_query('call AddDomainToComputer(' .$_GET['computer_id']. ', ' .$_GET['domain_id']. ')')
              or die(mysql_error());
            break;
            case 'removeDomain' :
              mysql_query('call RemoveDomainOfComputer(' .$_GET['computer_id']. ', ' .$_GET['domain_id']. ')')
              or die(mysql_error());
            break; 
            default:
              die('Unknown action '.$action);
          }
        }
        mysql_close($con);
      ?>
      
      <!-- The parametric part of above URL doesn't work -->
      <input type="hidden" id="computer_id" name="computer_id" value="<?php echo $id ?>"/>
      <input type="hidden" id="action" name="action" value="saveChanges"/>

      <table class="contentTable" cellspacing="0" border="1">
        <?php
          $con = get_connection();
          $result = mysql_query('call GetComputerDetail('.$id.')') or die( mysql_error() );
          
          if ($result)
          {  
            $row = mysql_fetch_array($result);
            mysql_close($con);

            echo('<tr><td>PC name: </td><td><input type="text" id="name" name="name" value="' . $row[1] . '"/></td></tr>');
            echo('<tr><td>Room: </td><td><select id="room_id" name="room_id">');
            $con = get_connection();
            $roomRes = mysql_query('call SelectAllRooms()') or die(mysql_error());
            while($roomRow = mysql_fetch_array($roomRes))
            {
              $sel = ($roomRow[0] == $row[2] ? 'selected="yes"' : '');
              echo '<option value="' .$roomRow[0]. '" ' . $sel . '>' .$roomRow[1]. '</option>';
            } 
             
            echo('</select></td></tr>');
            echo('<tr><td>Administrator: </td><td>' . $row[4] . '</td></tr>');            
            echo '<tr><td>Operational: </td><td><select id="operational" name="operational">';
            echo '<option value="1" ' .($row[5] == chr(0x01) ? 'selected="yes"' : ''). '>Yes</option>';
            echo '<option value="0" ' .($row[5] == chr(0x01) ? '' : 'selected="yes"'). '>No</option>';
            echo '</select></td></tr>';
            echo '<tr><td>Type: </td><td><select id="isServer" name="isServer">';
            echo '<option value="1" ' .($row[6] == chr(0x01) ? 'selected="yes"' : ''). '>Server</option>';
            echo '<option value="0" ' .($row[6] == chr(0x01) ? '' : 'selected="yes"'). '>Client</option>';
            echo '</select></td></tr>';
            
            $con = get_connection();
            $domRes = mysql_query('call GetDomainsOfComputer('.$id.')') or die(mysql_error());
            if($domRes)
            {
              echo('<tr><td>Included in domains: </td><td><table width="100%">');
              while($domRow = mysql_fetch_array($domRes))
              {
                echo '<tr><td width="90%">' . $domRow[1] . '</td><td><a href="ComputerDetails.php?action=removeDomain&computer_id=' . $id .
                     '&domain_id=' . $domRow[0] . '">exclude</a></td></tr>';
              }
              mysql_close($con);

              echo '<tr><td colspan="2"><a href="javascript: showHideDiv(\'netDomainsDiv\')">Add to a domain...</a>';
              
              echo '<br/><div id="netDomainsDiv" style="display: none; padding: 3px;"><br /><form name="domainForm">';
              echo '<select id="domain_id">';
              $con = get_connection();
              $allDomRes = mysql_query('call SelectAllNetDomains()') or die(mysql_error());
              while($allDomRow = mysql_fetch_array($allDomRes))
                echo '<option value="' .$allDomRow[0]. '">' .$allDomRow[1]. '</option>';

              echo '</select><input type="button" value="Add" '.
                   'onclick="javascript: location=\'./ComputerDetails.php?action=addDomain&computer_id='.$id.
                   '&domain_id=\'+document.getElementById(\'domain_id\').value;"/></div></form></table></td></tr>';

              mysql_close($con);
            }
            

            $con = get_connection();
            $hwRes = mysql_query('call GetHWComponentsOfComputer('.$id.')') or die (mysql_error());
            if($hwRes)
            {
              echo '<tr><td>Changes of hardware component: </td><td><table width="100%">';
              while($hwRow = mysql_fetch_array($hwRes))
              {
                echo '<tr><td width="75%">' .substr($hwRow[4], 0, 160).(strlen($hwRow[4])>160 ? '...' : ''). '</td><td width="15%">' .$hwRow[5]. 
                     '</td><td><a href="ComputerDetails.php?action=removeHW&computer_id=' .$id.
                     '&component_id=' .$hwRow[0]. '">remove</a></td></tr>';
              }
              echo '<tr><td colspan="2"><a href="./ComputerDetails_AddHWComponent.php?computer_id=' . $id . 
                   '">Add a component...</a> </td></tr></table></td></tr>';
            }
           	mysql_close($con);
           	
           	$con = get_connection();
           	
           	$swRes = mysql_query('call GetSoftwareOfComputer('.$id.')') or die (mysql_error());
           	if($swRes)
           	{
           	  echo '<tr><td>Installed software: </td><td><table width="100%">';
              while($swRow = mysql_fetch_array($swRes))
              {
                echo '<tr><td width="75%">' .$swRow[1]. '</td><td width="15%">' .$swRow[3]. 
                     '</td><td><a href="ComputerDetails.php?action=removeSW&computer_id=' .$id.
                     '&software_id=' .$swRow[0]. '">remove</a></td></tr>';
              }
              echo '<tr><td colspan="2"><a href="./ComputerDetails_AddSoftware.php?computer_id=' . $id . 
                   '">Add a software...</a> </td></tr></table></td></tr>';
            }
           	
           	mysql_close($con);

            echo('<tr style="background-color: #FFCC33;"><td colspan="2">Product details</td></tr>');
            echo('<tr><td>Coniguration: </td><td>' . $row[7] . '</td></tr>');
            echo('<tr><td>Purchase date: </td><td>' . $row[9] . '</td></tr>');
            echo('<tr><td>Warranty (months): </td><td>' . $row[10] . '</td></tr>');
            echo('<tr><td>Price (Eur): </td><td>' . $row[8] . ',-</td></tr>');
            echo('<tr><td>Supplier: </td><td>' . $row[11] . '</td></tr>');
          }
        ?>
      </table>
      <br /><br />
      <input type="submit" value="Save"/>&nbsp;&nbsp;
      <input type="button" value="Discard" onclick="javascript: location='../ComputersInRooms.php';"/>
    </form>
    <br /><br />
  </div>
</body>
</html>
